#**************************************************************************************#
#                                                                                      #
#                                                                                      #
#                       REPLICATION CODE FOR THE PAPER:                                #
#                                                                                      #
# National Personality Traits and Regime Type: A Cross-National Study of 47 Countries  #
#                                                                                      #
#                                                                                      #
#**************************************************************************************#
#
#***************#
# Load Packages #
#***************#

library(foreign)
library(xlsx)
library(lattice)
library(boot)
library(lavaan)

#**************************************************************************************#
#***************#
#   Load data   #
#***************#
setwd("F:\\~Joan Barcelo\\~Present\\~Washington U. in STL (PhD Political Science)\\2nd term\\Conflict\\ResearchPaper\\")

personality <- read.xlsx("JCCR_dataset.xlsx", sheetName = "Sel_importar")

personality <- personality[1:47,]

bs <- function(formula, data, indices) {
  d <- data[indices,] # allows boot to select sample 
  fit <- lm(formula, data=d)
  return(coef(fit)) 
}

set.seed(1234567890)

#*****************************#
#    Table 1: MAIN EFFECTS    #
#*****************************#

#    Column 1 : No controls   #

col1 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c)
print(col1)

#    Column 2 :  GDP #
col2 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + log(GDPpc05))
print(col2)

#    Column 3 : GINI  #
col3 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + Gini05)
print(col3)

#    Column 4 : Internet  #
col4 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + Internet05)
print(col4)

#    Column 5 : Disease  #
col5 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + Disease9)
print(col5)

#    Column 6 : Thermal Demands  #
col6 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + weather2)
print(col6)

#    Column 7 : Quality Index  #
col7 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + QI)
print(col7)

#    Column 8 : Acquiescence  #
col8 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + Acquies)
print(col8)

#    Column 9 : Thermal Demands*lnGDP  #
col9 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ e + o +  a + n + c + weather2*log(GDPpc05))
print(col9)


#*****************************#
#    Table 2: MECHANISMS      #
#*****************************#

#    Column 1 : Regress Democracy on Secular Values #

table2col1 <- boot(data=personality, statistic=bs, R=20000, formula=Vanhanen00 ~ sec_val)
print(table2col1)

#    Column 2 : Regress Secular Values on Traits #

table2col2 <- boot(data=personality, statistic=bs, R=10000, formula= sec_val ~ o + c + e + a + n)
table2col2

#    Column 3 : Regress Democracy on Secular Values and Traits #

table2col3 <- boot(data=personality, statistic=bs, R=20000, formula= Vanhanen00 ~ sec_val + o + c + e + a + n)
table2col3

#    Column 4 : Regress Democracy on Self-expression Values #

table2col4 <- boot(data=personality, statistic=bs, R=20000, formula= Vanhanen00 ~ self_val)
table2col4

#    Column 5 : Regress Self-expression Values on Traits #

table2col5 <- boot(data=personality, statistic=bs, R=20000, formula= self_val ~ o + c + e + a + n)
table2col5

#    Column 6 : Regress Democracy on Self-expression values and Traits #

table2col6 <- boot(data=personality, statistic=bs, R=20000, formula= Vanhanen00 ~ self_val + o + c + e + a + n)
table2col6


#*****************************************************************************#
#    In text information: Formal tests of Direct versus Indirect Channels     #
#*****************************************************************************#

model <-'
# outcome model
Vanhanen00 ~ z*o + y*c + w*e + v*a + b*n+u*trad_val

# mediator models
trad_val ~ t*o + s*c + r*e + q*a

# indirect effects
IDE := u*t

# direct effect
direct := z

# total effect
total := z+u*t
'

model01 <-'
# outcome model
Vanhanen00 ~ z*o + y*c + w*e + v*a + b*n+u*trad_val

# mediator models
trad_val ~ t*o + s*c + r*e + q*a

# indirect effects
IDE := u*q

# direct effect
direct := v

# total effect
total := v+u*q
'

model1 <-'
# outcome model
Vanhanen00 ~ z*o + y*c + w*e + v*a + b*n+u*trad_val

# mediator models
trad_val ~ t*o + s*c + r*e + q*a

# indirect effects
IDE := u*r

# direct effects
direct := w

# total effect
total := w+u*r
'
mod_OthroughT <- sem(model,data=personality,bootstrap = 20000)
summary(mod_OthroughT, fit.measures=TRUE, standardize=TRUE, rsquare=TRUE)
boot.fit1 <- parameterEstimates(mod_OthroughT, boot.ci.type="bca.simple",level=0.95, ci=TRUE,standardized = FALSE)
boot.fit1

mod_AthroughT <- sem(model01,data=personality,bootstrap = 20000)
summary(mod_AthroughT, fit.measures=TRUE, standardize=TRUE, rsquare=TRUE)
boot.fit01 <- parameterEstimates(mod_AthroughT, boot.ci.type="bca.simple",level=0.95, ci=TRUE,standardized = FALSE)
boot.fit01

mod_EthroughT <- sem(model1,data=personality,bootstrap = 20000)
summary(mod_OthroughT, fit.measures=TRUE, standardize=TRUE, rsquare=TRUE)
boot.fit2 <- parameterEstimates(mod_EthroughT, boot.ci.type="bca.simple",level=0.95, ci=TRUE,standardized = FALSE)
boot.fit2

model2 <-'
# outcome model
Vanhanen00 ~ z*o + y*c + w*e + v*a + b*n+u*eman_val

# mediator models
eman_val ~ t*o + s*c + r*e + q*a

# indirect effects
IDE := u*t

# direct effect
direct := z

# total effect
total := z+u*t
'

model3 <-'
# outcome model
Vanhanen00 ~ z*o + y*c + w*e + v*a + b*n +u*self_val

# mediator models
self_val ~ t*o + s*c + r*e + q*a

# indirect effects
IDE := u*r

# direct effect
direct := w

# total effect
total := w+u*r
'

mod_OthroughE <- sem(model2,data=personality,bootstrap = 20000)
summary(mod_OthroughE, fit.measures=TRUE, standardize=TRUE, rsquare=TRUE)
boot.fit2 <- parameterEstimates(mod_OthroughE, boot.ci.type="bca.simple",level=0.95, ci=TRUE,standardized = FALSE)
boot.fit2

mod_EthroughE <- sem(model3,data=personality,bootstrap = 20000)
summary(mod_EthroughE, fit.measures=TRUE, standardize=TRUE, rsquare=TRUE)
boot.fit3 <- parameterEstimates(mod_EthroughE, boot.ci.type="bca.simple",level=0.95, ci=TRUE,standardized = FALSE)
boot.fit3